﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TMDT_Nhom1A.DAO;
using TMDT_Nhom1A.DTO;

namespace TMDT_Nhom1A.BUS
{
    public class ElectricDevice_BUS
    {
        public static List<ElectricDevice_DTO> ListProductByParentCatalogue(int ParentCatalogueID)
        {
            return ElectricDevice_DAO.ListProductByParentCatalogue(ParentCatalogueID);
        }

        public static List<ElectricDevice_DTO> ListProductByChildCatalogue(int ChildCatalogueID)
        {
            return ElectricDevice_DAO.ListProductByChildCatalogue(ChildCatalogueID);
        }

        public static List<ElectricDevice_DTO> DetailElectricDevice(string ElectricDeviceID)
        {
            return ElectricDevice_DAO.DetailElectricDevice(ElectricDeviceID);
        }

        public static List<ElectricDevice_DTO> DetailElectricDeviceByCouponID(string CouponID)
        {
            return ElectricDevice_DAO.DetailElectricDeviceByCouponID(CouponID);
        }

        public static void CatalogueIDOfElectricDevice(string ElectricDeviceID, ref int ParentCatalogueID, ref int ChildCatalogueID)
        {
            ElectricDevice_DAO.CatalogueIDOfElectricDevice(ElectricDeviceID, ref ParentCatalogueID, ref ChildCatalogueID);
        }

        public static List<ElectricDevice_DTO> ListNewProduct()
        {
            return ElectricDevice_DAO.ListNewProduct();
        }

        public static List<ElectricDevice_DTO> ListUpdateProduct()
        {
            return ElectricDevice_DAO.ListUpdateProduct();
        }

        public static bool CheckNameElectricDevice(string ElectricDeviceName)
        {
            return ElectricDevice_DAO.CheckNameElectricDevice(ElectricDeviceName);
        }

        public static void DeleteElectricDevice(string ElectricDevice_ID)
        {
            ElectricDevice_DAO.DeleteElectricDevice(ElectricDevice_ID);
            List<COUPON> listcoupon = Coupon_BUS.ListCouponByElectricDeviceID(ElectricDevice_ID);
            for (int i = 0; i < listcoupon.Count; i++)
            {
                Coupon_BUS.UpdateStatusCoupon(listcoupon[i].Coupon_ID, 0);
            }
        }

        public static void AddProduct(ELECTRIC_DEVICE electricdevice)
        {
            try
            {
                ElectricDevice_DAO.AddProduct(electricdevice);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void UpdateElectricDevice(ELECTRIC_DEVICE electricdevice)
        {
            ElectricDevice_DAO.UpdateElectricDevice(electricdevice);
        }

        public static List<CURRENCY> GetCurrencyOfProduct()
        {
            return ElectricDevice_DAO.GetCurrencyOfProduct();
        }

        public static List<ElectricDevice_DTO> FastSearch(string keyword)
        {
            return ElectricDevice_DAO.FastSearch(keyword);
        }

        public static List<ElectricDevice_DTO> SearchElectricDevice(string keyword, int check, string kindofsearch, decimal minprice, decimal maxprice, string currency, int keycatalogueid, int brandid, int flag)
        {
            List<ElectricDevice_DTO> listproduct = new List<ElectricDevice_DTO>();
            List<ElectricDevice_DTO> listproducttemp = new List<ElectricDevice_DTO>();
            if (keycatalogueid == 0)
            {
                listproducttemp = ElectricDevice_DAO.ListAllProduct();
            }
            else
            {
                listproducttemp = ElectricDevice_DAO.ListProductByParentCatalogue(keycatalogueid);
            }
            

            for (int i = 0; i < listproducttemp.Count; i++)
            {
                int count = 0;
                if (keyword != "")
                {
                    if (check == 0)
                    {
                        string[] strtemp = keyword.Split(' ');
                        for (int j = 0; j < strtemp.Count(); j++)
                        {
                            if (kindofsearch == "Tất cả")
                            {
                                if (listproducttemp[i].ElectricDevice_Name.Contains(strtemp[j]) || listproducttemp[i].Description.Contains(strtemp[j]))
                                {
                                    count++;
                                    break;
                                }
                            }
                            if (kindofsearch == "Tên sản phẩm")
                            {
                                if (listproducttemp[i].ElectricDevice_Name.Contains(strtemp[j]))
                                {
                                    count++;
                                    break;
                                }
                            }
                            if (kindofsearch == "Mô tả sản phẩm")
                            {
                                if (listproducttemp[i].Description.Contains(strtemp[j]))
                                {
                                    count++;
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (kindofsearch == "Tất cả")
                        {
                            if (listproducttemp[i].ElectricDevice_Name.Contains(keyword) || listproducttemp[i].Description.Contains(keyword))
                            {
                                count++;
                            }
                        }
                        if (kindofsearch == "Tên sản phẩm")
                        {
                            if (listproducttemp[i].ElectricDevice_Name.Contains(keyword))
                            {
                                count++;
                            }
                        }
                        if (kindofsearch == "Mô tả sản phẩm")
                        {
                            if (listproducttemp[i].Description.Contains(keyword))
                            {
                                count++;
                            }
                        }
                    }
                }
                if (minprice > 0 && decimal.Parse(listproducttemp[i].RetailPrice) >= minprice)
                {
                    count++;
                }

                if (maxprice > 0 && decimal.Parse(listproducttemp[i].RetailPrice) <= maxprice)
                {
                    count++;
                }

                if (currency == "Tất cả")
                {
                    count++;
                }
                else
                {
                    if (listproducttemp[i].Currency_ID == currency)
                    {
                        count++;
                    }
                }

                if (brandid == 0)
                {
                    count++;
                }
                else
                {
                    if (listproducttemp[i].Brand_ID == brandid)
                    {
                        count++;
                    }
                }

                if (count == flag)
                {
                    listproduct.Add(listproducttemp[i]);
                }
            }

            return listproduct;
        }

    }
}
